//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.hecai.scanning.thread;

import com.hecai.scanning.config.ConfigProperties;
import com.hecai.scanning.handler.ScannerHandler;
import com.hecai.scanning.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;

@Component
public class BillingRedemption extends Thread
{
    private static final Logger log = LoggerFactory.getLogger(BillingRedemption.class);

    private LinkedBlockingQueue<String> ticketIdsQueue = ScannerHandler.getTicketIDQueue();

    @Autowired
    private ConfigProperties properties;

    public void run()
    {
        while (true)
        {
            while (true)
            {
                try
                {
                    String ticketIds = this.ticketIdsQueue.take();
                    ticketIds = ticketIds.replace("\n", "").replace("\r", "");
                    String ticketIdLength = properties.getTicketIdLength();
                    String[] lengths = ticketIdLength.split("~");
                    int min = Integer.parseInt(lengths[0]);
                    int max = Integer.parseInt(lengths[1]);
                    if (ticketIds.length() < min || ticketIds.length() > max)
                    {
                        log.info("扫描的数据长度有误，不能操作。");
                    } else
                    {
                        String centerIpAddr = properties.getCenterIpAddr();
                        String recvTickets = properties.getRecvTickets();
                        String hallNumber = properties.getHallNumber();
                        String windowNumber = properties.getWindowNumber();
                        Map params = new HashMap();
                        params.put("ticketId", ticketIds);
                        params.put("hallNumber", hallNumber);
                        params.put("windowNumber", windowNumber);
                        log.info("发送票号操作，票号:{},大厅:{},窗口:{}", new Object[]{ticketIds, hallNumber, windowNumber});
                        String result = HttpUtil.doPost(centerIpAddr + recvTickets, params);
                        if (result == null)
                        {
                            log.error("发送票号失败，票号为:{}", ticketIds);
                        } else
                        {
                            result = result.trim()
                                    .replace("\n", "")
                                    .replace("\t", "")
                                    .replace("\r", "")
                                    .replace("\"", "");
                            switch (result)
                            {
                                case "EMPTY":
                                    log.error("后台系统接收到的票号为空，票号：{}", ticketIds);
                                    break;
                                case "WINERR":
                                    log.error("当前窗口或大厅在后台系统中不存在，大厅：{}，窗口：{}", hallNumber, windowNumber);
                                    break;
                                case "FALSE":
                                    log.error("更新订单信息失败");
                                    break;
                                case "ORDERR":
                                    log.error("当前大厅和窗口未分配到订单，大厅：{}，窗口：{}", hallNumber, windowNumber);
                                    break;
                                case "EXIST":
                                    log.error("票号已发送成功，请勿重复操作");
                                    break;
                                case "NOLOG":
                                    log.error("用户登录信息失效，请重新登录");
                                    break;
                                case "ERRTI":
                                    log.error("扫描的彩种和面值与订单不一致，请核对并重新扫描！");
                                    break;
                                case"TICEXI":
                                    log.error("票号已存在，请核对并重新扫描！");
                                    break;
                                case"STOEMP":
                                    log.error("库存中不含此票号，或此票未分配！");
                                    break;
                                default:
                                    log.info("票号发送成功，票号：{}", ticketIds);
                                    break;
                            }
                        }
                    }
                } catch (Exception var11)
                {
                    log.error("Send request has a error.{}", var11);
                }
            }
        }
    }
}
